function myPromise(fn) {
  let self = this;
  self.status = 'pending';
  self.value = undefined;//状态为resolved的值
  self.reason = undefined;//状态为rejected的值
  function resolve(value) {
    if (self.status = 'pending') {
      self.value = value;
      self.status = 'resolved'
    }
  };
  function rejected(value) {
    if (self.status = 'pending') {
      self.value = value;
      self.status = 'rejected'
    }
  };
  try {
    fn(resolve, rejected)
  } catch (error) {
    rejected(error)
  }
}
myPromise.prototype.then = function (onFullfilled, onRejected) { //onFullfilled 完成后执行的函数 onRejected 失败后执行的函数 
  let self = this;
  switch (self.status) {
    case "resolved":
      onFullfilled(self.value);
      break;
    case "rejected":
      onRejected(self.reason);
      break;
    default:
  }
}
let p1 = new myPromise(
  function (resolve, reject) { resolve(1) }
)
p1.then(res => console.log(res))


function MyPromise(fn) {
  this.status = 'pending';
  this.value = undefined;//状态为resolved的值
  this.reason = undefined;//状态为rejected的值
  const resolve = (value) => {
    console.log(this);
    this.value = value
    this.status = 'resolved';
  }
  const reject = (reason) => {
    this.reason = reason
    this.status = 'rejected';
  }
  try {
    fn(resolve, reject)
  } catch (error) {
    reject(error)
  }
}
MyPromise.prototype.then = function (onresolve, onreject) {

  switch (this.status) {
    case 'resolved':
      console.log(this);
      onresolve(this.value)
      break;

    default:
      onreject(this.reason)
      break;
  }

}
let p2 = new MyPromise(
  function (resolve, reject) { reject(1) }
)
p2.then(res => console.log(res), res => console.log(res))


const obj = {
  a: 1,
  b: 2
}
for (const key in obj) {
  obj[key] = obj[key] + 1
}

for (const key of [1, 2]) {
  console.log(key);
}



class MPromise {
  status = 'pending';
  value = undefined;//状态为resolved的值
  reason = undefined;//状态为rejected的值

  constructor(fn) {
    const resolve = (value) => {
      console.log(this);
      this.value = value
      this.status = 'resolved';
    }
    const reject = (reason) => {
      this.reason = reason
      this.status = 'rejected';
    }
    try {
      fn(resolve, reject)
    } catch (error) {
      reject(error)
    }
  }
  then(onresolve, onreject) {
    switch (this.status) {
      case 'resolved':
        console.log(this);
        onresolve(this.value)
        break;

      default:
        onreject(this.reason)
        break;
    }
  }

}

let p3 = new MPromise(
  function (resolve, reject) { reject(1) }
)
p3.then(res => console.log(res), res => console.log(res))